<style>
    #add {
        padding: 20px 25px 25px 0;
    }

    #add .layui-treeSelect .ztree li a, .ztree li span {
        margin: 0 0 2px 3px !important;
    }
</style>
<div class="layui-fluid" id="shareUpdate">
    <form class="layui-form" action="" lay-filter="share-update-form" enctype="multipart/form-data" id="share-update-form">

        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">标题</label>
            <div class="layui-input-block">
                <input type="text" autocomplete="off" name="title" lay-verify="required" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item" style="display: none">
            <label class="layui-form-label febs-form-item-require">分享id</label>
            <div class="layui-input-block">
                <input type="text" name="shareId"  class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-form-item">
                <label class="layui-form-label">分享头图</label>
                <img alt="头像" style="width: 200px;height: 160px" title="点我更换头像" data-th-src="@{${share.imgUrl}}">
                <div class="layui-input-block" style="margin-top: 5px">
                    <button type="button" class="layui-btn" id="upload_headPhoto">
                        <i class="layui-icon">&#xe67c;</i>上传活动头图
                    </button>
                    <div id="result_headPhoto"></div>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">分享描述：</label>
            <div class="layui-input-block">
                <input type="text" name="description" autocomplete="off" lay-verify="required" class="layui-input">
            </div>
        </div>


        <div class="layui-form-item febs-hide">
            <button class="layui-btn" lay-submit="" lay-filter="shareUpdate-form-submit" id="submit"></button>
            <button type="reset" class="layui-btn" id="reset"></button>
            <input type="hidden" name="test">
        </div>
    </form>
</div>
<script data-th-inline="javascript">
    layui.use(['febs', 'form', 'layedit', 'validate', 'upload'], function () {
        var $ = layui.$,
            febs = layui.febs,
            layer = layui.layer,
            form = layui.form,
            $view = $('#shareUpdate'),
            share = [[${share}]],
            laydate = layui.laydate,
            upload = layui.upload,
            validate = layui.validate;

        form.verify(validate);
        form.render();

        //获取活动头图名称
        if (share.imgUrl != null) {
            var ss = share.imgUrl.split('/');
            var dd = ss[ss.length - 1];
            $("#result_headPhoto").html(dd);
        }

        initUserValue();

        function initUserValue() {
            form.val("share-update-form", {
                "shareId":share.shareId,
                "title": share.title,
                "description":share.description,
                "activityId":share.activityId,
                "limitNumber":share.limitNumber,
                "status": share.status,
            });
        }



        upload.render({
            elem: '#upload_headPhoto', //绑定元素
            accept: "images",
            field: "headPhoto_file",
            acceptMime: 'image/*',//打开文件选择框时,只显示图片文件
            auto: false,  //是否选完文件后自动上传。默认值：true
            // bindAction: '#btnSub',
            choose: function (obj) {
                obj.preview(function (index, file, result) {
                    // $('#headImage').attr('src', result);
                    $("#result_headPhoto").html(file.name);
                })
            }
        });



        form.on('submit(shareUpdate-form-submit)', function (data) {
            var formData = new FormData(document.getElementById("share-update-form"));
            console.log(data.field)
            $.ajax({
                url: ctx + 'share/updateShare',
                data: formData,
                type: 'post',
                dataType: 'json',
                processData: false,
                contentType: false,
                success: function (data) {
                    if (data.code == 200) {
                        layer.msg('操作成功', {
                            icon: 6,
                            time: 1000
                        }, function () {
                            layer.closeAll();
                            $('#febs-share').find('#reset').click();
                        });
                    } else {
                        layer.msg('操作失败', {
                            icon: 5,
                            time: 1000
                        });
                    }
                },
                error: function (data) {
                    layer.msg('操作失败', {
                        icon: 5,
                        time: 1000
                    });
                }
            });
            return false;
        });
    });
</script>